import pandas as pd
url = "D:/pythondate/work1.csv"
df = pd.read_csv(url)

# printing the first 5 rows of the dataset
print(df.head(5))

# Let's visualize the distribution of the penguins species with a bar plot in matplotlib
import matplotlib.pyplot as plt

plt.bar(df['Species'], df['BodyMass'])
plt.xlabel('Species')
plt.ylabel('BodyMass')
plt.title('Distribution of Penguin Species by BodyMass')
plt.show()

# Let's visualize with boxplots how the FlipperLength, CulmenLength and CulmenDepth are distributed for each species
# importing seaborn
import seaborn as sns

# Assuming the columns for FlipperLength, CulmenLength, and CulmenDepth are named accordingly
sns.boxplot(x='Species', y='FlipperLength', data=df)
plt.xlabel('Species')
plt.ylabel('FlipperLength')
plt.title('Distribution of Penguin Species by FlipperLength')
plt.show()

sns.boxplot(x='Species', y='CulmenLength', data=df)
plt.xlabel('Species')
plt.ylabel('CulmenLength')
plt.title('Distribution of Penguin Species by CulmenLength')
plt.show()

sns.boxplot(x='Species', y='CulmenDepth', data=df)
plt.xlabel('Species')
plt.ylabel('CulmenDepth')
plt.title('Distribution of Penguin Species by CulmenDepth')
plt.show()

# Show rows with missing values
print(df[df.isnull().any(axis=1)])

# Drop rows with missing values
df = df.dropna()

# Let's prepare for training:
# 1. Split the data into features and labels
# 2. Split the data into training and test sets

# Split the data into features and labels
# features are CulmenLength, CulmenDepth, FlipperLength
# labels are Species
# features are X, labels are y

from sklearn.model_selection import train_test_split

# Split the data into features and labels
features = df[['CulmenLength', 'CulmenDepth', 'FlipperLength']]
labels = df['Species']

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.3, random_state=42)

# Print the shapes of the training and test sets
print("Training set shape:", X_train.shape, y_train.shape)
print("Test set shape:", X_test.shape, y_test.shape)

# Split the data into training and test sets in a way to have 30% of the data for testing
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.3, random_state=42)

# Let's train a Logistic Regression model
# 1. Create a multiclass Logistic Regression model
# 2. Train the model

# Create a multiclass Logistic Regression model with increased max_iter
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)

# Train the model
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

# Print the first few predictions
print("First few predictions:", y_pred[:5])

# Calculate the accuracy of the model
from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of the model:", accuracy)

# Let's evaluate the model
# 1. Predict the labels of the test set
# 2. Calculate the accuracy of the model

# Predict on the test set
y_pred = model.predict(X_test)

# Print the first few predictions
print("First few predictions:", y_pred[:5])

# Calculate the accuracy of the model
from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of the model:", accuracy)
